package listbyorder.access201_300.test279;

import java.util.HashMap;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/6/27 13:06
 */
public class Solution4 {

    // 解法三的map优化 __ 过了
    public int numSquares(int n) {
        HashMap<Integer, Integer> map = new HashMap<>();
        return numSquaresHelper(n, map);
    }

    private int numSquaresHelper(int n, HashMap<Integer, Integer> map) {
        if (n == 0) return 0;
        if (map.containsKey(n)) {
            return map.get(n);
        }
        int count = Integer.MAX_VALUE;
        for (int i = 1; i * i <= n; i++) {
            count = Math.min(count, numSquaresHelper(n - i * i, map) + 1);
        }
        map.put(n, count);
        return count;
    }
}
